//
//  KXDetailController.swift
//  KXBook
//
//  Created by  on 2022/9/10.
//

import UIKit

class KXDetailController: KXViewController {
    @IBOutlet var collectionView: UICollectionView!
    var model : KXRecord?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = LS("查看明细")
        self.collectionView.register(KXDetailImgCell.nib(), forCellWithReuseIdentifier: KXDetailImgCell.ID())
        self.collectionView.register(KXDetailHeader.nib(), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: KXDetailHeader.ID())
    }
    
    @IBAction func actionForBtn(_ sender: UIButton) {
        if sender.tag == 201{
            let str = String(format: "where %@=%@ and %@=%@", bg_sqlKey("userID"),bg_sqlValue(APP.user.id),bg_sqlKey("stamp"),bg_sqlValue("\(self.model!.stamp)"))
            KXRecord.bg_deleteAsync(nil, where: str) { success in
                DispatchQueue.main.async {
                    NotifyTool.post(NotifyName.updateRecord)
                    self.backBtnAction()
                }
            }
        }else if sender.tag == 202{
            let vc = KXAddViewController()
            vc.model = self.model!
            self.push(vc, true)
        }
    }
}

//MARK: - UICollectionViewDataSource
extension KXDetailController : UICollectionViewDataSource
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier:KXDetailImgCell.ID(), for: indexPath) as! KXDetailImgCell
        cell.model = self.model!
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let view = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: KXDetailHeader.ID(), for: indexPath) as! KXDetailHeader
        view.model = self.model!
        return view
    }
    
}
//MARK: - UICollectionViewDelegate
extension KXDetailController : UICollectionViewDelegate
{
}

//MARK: - UICollectionViewDelegateFlowLayout
extension KXDetailController : UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {
        let h = (kUIScreenWidth - 20 * 4) / 1
        return CGSize(width: kUIScreenWidth - 40, height: h)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat
    {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat
    {
        return 0
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
    {
        return UIEdgeInsets(top:20, left: 20, bottom: 20, right: 20)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width:kUIScreenWidth, height:280)
    }
}
